package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StateCode;
import cn.tedu._04weibo.mapper.UserMapper;
import cn.tedu._04weibo.pojo.dto.UserLoginParam;
import cn.tedu._04weibo.pojo.dto.UserRegParam;
import cn.tedu._04weibo.pojo.entity.User;
import cn.tedu._04weibo.pojo.vo.UserSelectQuery;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/users/reg/")
@Slf4j
@Api(tags = "01.用户管理")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("reg")
    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 10)
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam){
        log.debug("userRegParam:"+userRegParam);
        String username = userRegParam.getUsername();
        UserSelectQuery userSelectQuery=userMapper.UsernameQuery(username);
        //先看用户名是否被占用用了
        if (userSelectQuery!=null){
            return new JsonResult(StateCode.USERNAME_EXIST,userSelectQuery);
        }
        //如果没有被占用，那就开始执行注册的操作
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        //user.setId(userRegParam.getId());
        userMapper.UserRrg(user);
        log.debug("user:"+user);
        return JsonResult.ok(user);
    }
    //用户登录功能
    @ApiOperation(("用户登录"))
    @ApiOperationSupport(order = 20)
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam ,@ApiIgnore HttpSession session){
        String username = userLoginParam.getUsername();
        String password = userLoginParam.getPassword();
        UserSelectQuery userSelectQuery=userMapper.UserLogin(username,password);
        //查看用户名和密码是否正确
        if (userSelectQuery!=null){
            //要把当前用户信息存入到userSelectQuery里面去
            session.setAttribute("user",userSelectQuery);
            return JsonResult.ok(userSelectQuery);
        }
        return new JsonResult(StateCode.USERNAME_PASSWORD_ERROR);
    }
    //查看当前用户
    //要判断是否登录了，如果没有登录则先登录
    @ApiOperation("查看当前用户")
    @ApiOperationSupport(order = 30)
    @GetMapping("currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        UserSelectQuery  currentUser = (UserSelectQuery ) session.getAttribute("user");
//        if (currentUser==null){
//            return new JsonResult(StateCode.NOT_LOGIN);
//        }
        return JsonResult.ok(currentUser);
    }
    //退出登录功能
    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 40)
    @GetMapping("logout")
    public void logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
    }
}




























